﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YLW.Plat.Jhfw.HL7V3.XSDModels.PRPA_IN201314UV02;
using YLW.Plat.Jhfw.Model.JhfwHzgl;

namespace YLW.Plat.Jhfw.HL7V3.V3Message
{
    public class V3_PRPA_IN201314UV02
    {
        public static string PatientInfoUpdate(AddJhfwHzglIn jsonData, string id_extension, string sender_name, string receiver_name)
        {
            string hl7Xml = string.Empty;

            if (jsonData == null)
            {
                return hl7Xml;
            }

            //根据XML XSD构建HL7 XML消息格式

            //性别
            string sexCode = "";
            string sexName = "";
            switch (jsonData.psex)
            {
                case "B":
                    sexCode = "9";
                    sexName = "未说明的性别";
                    break;
                case "F":
                    sexCode = "2";
                    sexName = "女性";
                    break;
                case "M":
                    sexCode = "1";
                    sexName = "男性";
                    break;
                case "U":
                    sexCode = "0";
                    sexName = "未知的性别";
                    break;
            }

            //婚姻状况
            string mstatusCode = "";
            string mstatusName = "";
            switch (jsonData.pmstatus)
            {
                case "1":
                    mstatusCode = "10";
                    mstatusName = "未婚";
                    break;
                case "2":
                    mstatusCode = "20";
                    mstatusName = "已婚";
                    break;
                case "3":
                    mstatusCode = "30";
                    mstatusName = "丧偶";
                    break;
                case "4":
                    mstatusCode = "40";
                    mstatusName = "离婚";
                    break;
                case "9":
                    mstatusCode = "90";
                    mstatusName = "未说明的婚姻情况";
                    break;
            }

            //联系人关系
            string ktopName = "";
            switch (jsonData.ktop)
            {
                case "0":
                    ktopName = "患者本人";
                    break;
                case "1":
                    ktopName = "配偶";
                    break;
                case "2":
                    ktopName = "子";
                    break;
                case "3":
                    ktopName = "女";
                    break;
                case "4":
                    ktopName = "孙子、孙女、或外孙子、外孙女";
                    break;
                case "5":
                    ktopName = "父母";
                    break;
                case "6":
                    ktopName = "祖父母或外祖父母";
                    break;
                case "7":
                    ktopName = "兄、弟、姐、妹";
                    break;
                case "8":
                    ktopName = "其他";
                    break;
            }

            //创建根对象
            var xmlRoot = new PRPA_IN201314UV02
            {
                ITSVersion = "XML_1.0",
                id1 = new id1 { root = "2.16.156.10011.2.5.1.1", extension = id_extension },
                creationTime = new creationTime { value = DateTime.Now.ToString("yyyyMMddHHmmss") },
                interactionId = new interactionId { root = "2.16.156.10011.2.5.1.2", extension = "PRPA_IN201314UV02" },
                processingCode = new processingCode { code = "P" },
                processingModeCode = new processingModeCode(),
                acceptAckCode = new acceptAckCode { code = "AL" },
                receiver = new receiver
                {
                    typeCode = "RCV",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id1 = new id1
                        {
                            item = new item
                            {
                                root = "2.16.156.10011.2.5.1.3",
                                extension = receiver_name
                            }
                        }
                    }
                },
                sender = new sender
                {
                    typeCode = "SND",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id1 = new id1
                        {
                            item = new item
                            {
                                root = "2.16.156.10011.2.5.1.3",
                                extension = sender_name
                            }
                        }
                    }
                },
                controlActProcess = new controlActProcess
                {
                    classCode = "CACT",
                    moodCode = "EVN",
                    subject = new subject
                    {
                        typeCode = "SUBJ",
                        registrationRequest = new registrationRequest
                        {
                            classCode = "REG",
                            moodCode = "RQO",
                            statusCode = new statusCode { code = "active" },
                            subject1 = new subject1
                            {
                                typeCode = "SBJ",
                                patient = new patient
                                {
                                    classCode = "PAT",
                                    id1 = new id1
                                    {
                                        item = new item { root = "2.16.156.10011.2.5.1.4", extension = jsonData.mrn }
                                    },
                                    statusCode = new statusCode { code = "active" },
                                    effectiveTime = new effectiveTime
                                    {
                                        any = new any
                                        {
                                            value = Convert.ToDateTime(jsonData.in_date).ToString("yyyyMMddHHmmss")
                                        }
                                    },
                                    patientPerson = new patientPerson
                                    {
                                        id1 = new id1
                                        {
                                            item = new item
                                            {
                                                root = "2.16.156.10011.1.3",
                                                extension = jsonData.pcid
                                            }
                                        },
                                        idCategory = new idCategory
                                        {
                                            code = "01",
                                            codeSystem = "2.16.156.10011.2.3.1.1",
                                            codeSystemName = "身份证件类别代码表",
                                            displayName = new displayName { value = "居民身份证" }
                                        },
                                        name = new name
                                        {
                                            item = new item
                                            {
                                                Items = new object[]
                                                {
                                                    new part{  value=jsonData.pname }
                                                }
                                            }
                                        },
                                        telecom = new telecom
                                        {
                                            item = new item { value = jsonData.pphone }
                                        },
                                        administrativeGenderCode = new administrativeGenderCode
                                        {
                                            code = sexCode,
                                            codeSystem = "2.16.156.10011.2.3.3.4",
                                            codeSystemName = "生理性别代码表(GB/T 2261.1)",
                                            displayName = new displayName
                                            {
                                                value = sexName
                                            }
                                        },
                                        birthTime = new birthTime { value = jsonData.pbirthday },
                                        addr = new addr
                                        {
                                            item = new item
                                            {
                                                Items = new object[]
                                                {
                                                    new part{ type="SAL",value=jsonData.paddress },
                                                    new part{ type="STA",value=jsonData.addr_province },
                                                    new part{ type="CTY",value=jsonData.addr_city },
                                                    new part{ type="CNT",value=jsonData.addr_area },
                                                    new part{ type="STB",value=jsonData.addr_town },
                                                    new part{ type="STR",value=jsonData.addr_village },
                                                    new part{ type="BNR",value=jsonData.addr_house },
                                                    new part{ type="ZIP",value=jsonData.pzip }
                                                }
                                            }
                                        },
                                        maritalStatusCode = new maritalStatusCode
                                        {
                                            code = mstatusCode,
                                            codeSystem = "2.16.156.10011.2.3.3.5",
                                            codeSystemName = "婚姻状况代码表(GB/T 2261.2)",
                                            displayName = new displayName { value = mstatusName }
                                        },
                                        ethnicGroupCode = new ethnicGroupCode
                                        {
                                            item = new item
                                            {
                                                code = jsonData.prace,
                                                codeSystem = "2.16.156.10011.2.3.3.3",
                                                codeSystemName = "民族类别代码表(GB 3304)",
                                                Items = new object[]
                                                {
                                                    new displayName { value = "" }
                                                }
                                            }
                                        },
                                        asEmployee = new asEmployee
                                        {
                                            classCode = "EMP",
                                            occupationCode = new occupationCode
                                            {
                                                code = jsonData.poccu,
                                                codeSystem = "2.16.156.10011.2.3.3.7",
                                                codeSystemName = "从业状况(个人身体)代码表(GB/T 2261.4)",
                                                displayName = new displayName { value = "" }
                                            },
                                            employerOrganization = new employerOrganization
                                            {
                                                classCode = "ORG",
                                                determinerCode = "INSTANCE",
                                                name = new name
                                                {
                                                    item = new item
                                                    {
                                                        Items = new object[]
                                                        {
                                                            new part{ value=jsonData.unit }
                                                        }
                                                    }
                                                },
                                                contactParty = new contactParty
                                                {
                                                    telecom = new telecom
                                                    {
                                                        item = new item { value = jsonData.unit_phone }
                                                    }
                                                }
                                            }
                                        },
                                        asOtherIDs = new asOtherIDs[]
                                        {
                                            new asOtherIDs
                                            {
                                                classCode="PAT",
                                                id1=new id1
                                                {
                                                    item=new item{root="2.16.156.10011.1.19",extension=jsonData.jzkh}
                                                }
                                            },
                                            new asOtherIDs
                                            {
                                                classCode="PAT",
                                                id1=new id1
                                                {
                                                    item=new item{root="2.16.156.10011.1.2",extension=jsonData.jzkh}
                                                },
                                                scopingOrganization=new scopingOrganization
                                                {
                                                    id1=new id1
                                                    {
                                                        item=new item{root="2.16.156.10011.1.5",extension=jsonData.jzkh}
                                                    },
                                                }
                                            }
                                        },
                                        personalRelationship = new personalRelationship
                                        {
                                            code = new code
                                            {
                                                code1 = jsonData.ktop,
                                                codeSystem = "2.16.156.10011.2.3.1.34",
                                                codeSystemName = "家庭关系代码表(GB/T 4761)",
                                                displayName = new displayName { value = ktopName }
                                            },
                                            telecom = new telecom
                                            {
                                                item = new item { value = jsonData.kin }
                                            },
                                            relationshipHolder1 = new relationshipHolder1
                                            {
                                                classCode = "PSN",
                                                determinerCode = "INSTANCE",
                                                name = new name
                                                {
                                                    item = new item
                                                    {
                                                        Items = new object[]
                                                        {
                                                            new part{ value=jsonData.kinphone }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    },
                                    providerOrganization = new providerOrganization
                                    {
                                        id1 = new id1
                                        {
                                            item = new item { root = "2.16.156.10011.1.5", extension = "" }
                                        },
                                        name = new name
                                        {
                                            item = new item
                                            {
                                                Items = new object[]
                                                {
                                                    new part { value="" }
                                                }
                                            }
                                        }
                                    },
                                    coveredPartyOf = new coveredPartyOf
                                    {
                                        typeCode = "COV",
                                        coverageRecord = new coverageRecord
                                        {
                                            classCode = "COV",
                                            moodCode = "EVN",
                                            beneficiary = new beneficiary
                                            {
                                                typeCode = "BEN",
                                                beneficiary1 = new beneficiary
                                                {
                                                    classCode = "MBR",
                                                    code = new code
                                                    {
                                                        code1 = jsonData.service,
                                                        codeSystem = "",
                                                        codeSystemName = "2.16.156.10011.2.3.1.248",
                                                        displayName = new displayName { value = jsonData.service == "2" ? "自费" : "浙江省医保" }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            author = new author
                            {
                                typeCode = "AUT",
                                assignedEntity = new assignedEntity
                                {
                                    classCode = "ASSIGNED",
                                    id1 = new id1
                                    {
                                        item = new item
                                        {
                                            root = "2.16.156.10011.1.4",
                                            extension = jsonData.in_empid
                                        }
                                    },
                                    assignedPerson = new assignedPerson
                                    {
                                        name = new name
                                        {
                                            item = new item
                                            {
                                                Items = new object[]
                                                {
                                                    new part{ value="" }
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            extension = new extension
                            {
                                dotype = new value { value1 = jsonData.dotype },
                                sjly = new value { value1 = jsonData.sjly },
                                lyxt = new value { value1 = jsonData.lyxt },
                                hoscode = new value { value1 = jsonData.hoscode },
                                mrn = new value { value1 = jsonData.mrn },
                                pname = new value { value1 = jsonData.pname },
                                psex = new value { value1 = jsonData.psex },
                                zjlx = new value { value1 = jsonData.zjlx },
                                pcid = new value { value1 = jsonData.pcid },
                                pbirthday = new value { value1 = jsonData.pbirthday },
                                prace = new value { value1 = jsonData.prace },
                                poccu = new value { value1 = jsonData.poccu },
                                pmstatus = new value { value1 = jsonData.pmstatus },
                                pphone = new value { value1 = jsonData.pphone },
                                paddress = new value { value1 = jsonData.paddress },
                                addr_province = new value { value1 = jsonData.addr_province },
                                addr_city = new value { value1 = jsonData.addr_city },
                                addr_area = new value { value1 = jsonData.addr_area },
                                addr_town = new value { value1 = jsonData.addr_town },
                                addr_village = new value { value1 = jsonData.addr_village },
                                addr_house = new value { value1 = jsonData.addr_house },
                                pzip = new value { value1 = jsonData.pzip },
                                jzkh = new value { value1 = jsonData.jzkh },
                                service = new value { value1 = jsonData.service },
                                unit = new value { value1 = jsonData.unit },
                                unit_phone = new value { value1 = jsonData.unit_phone },
                                ktop = new value { value1 = jsonData.ktop },
                                kin = new value { value1 = jsonData.kin },
                                kinphone = new value { value1 = jsonData.kinphone },
                                useflag = new value { value1 = jsonData.useflag },
                                in_empid = new value { value1 = jsonData.in_empid },
                                in_date = new value { value1 = jsonData.in_date },
                                xg_empid = new value { value1 = jsonData.xg_empid },
                                xgsj = new value { value1 = jsonData.xgsj }
                            }
                        }
                    }
                }
            };

            hl7Xml = HL7V3Helper.SerializeToXml(xmlRoot);

            return hl7Xml;
        }
    }
}
